package com.time.workshop.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.time.workshop.API;
import com.time.workshop.App;
import com.time.workshop.BaseConstant;
import com.time.workshop.BaseFragment;
import com.time.workshop.R;
import com.time.workshop.injector.Injector;
import com.time.workshop.injector.V;
import com.time.workshop.ui.CustomMadeActivity;
import com.time.workshop.ui.DingZuoActivity;
import com.time.workshop.ui.JieSongActivity;
import com.time.workshop.ui.LaoRenListActivity;
import com.time.workshop.ui.RunActivity;
import com.time.workshop.ui.WaiMaiOrderActivity;
import com.time.workshop.ui.XuYuanActivity;
import com.time.workshop.view.AutoInfiniteViewPager;
import com.time.workshop.view.TabHeadView;

//import com.time.workshop.ui.XuYuanActivity;

public class HeadFragment extends BaseFragment implements OnClickListener
{

	@V
	private AutoInfiniteViewPager vp_head;
	@V
	private FrameLayout fl_head;
	@V
	private LinearLayout ll_head_point;
	@V
	private TextView waimai, dingzuo, laoren, huodong, chongwu, tongcheng, guahao, peipao;
	@V
	private TabHeadView thv_head_title;

	private ArrayList<String> urls = new ArrayList<String>();

	private Activity activity;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		return inflater.inflate(R.layout.fragment_head, container, false);
	}

	@SuppressWarnings("deprecation")
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState)
	{
		super.onViewCreated(view, savedInstanceState);
		Injector.getInstance().inject(activity, this, view);
		init();
	}

	private void init()
	{
		thv_head_title.getLeftButton().setBackgroundResource(R.drawable.btn_title_left_bg_selector);
		thv_head_title.getRightButton().setVisibility(View.GONE);
		thv_head_title.getRightImageView().setVisibility(View.GONE);
		thv_head_title.setTitleLogo(R.drawable.ico_logo);

		waimai.setOnClickListener(this);
		dingzuo.setOnClickListener(this);
		guahao.setOnClickListener(this);
		tongcheng.setOnClickListener(this);
		laoren.setOnClickListener(this);
		chongwu.setOnClickListener(this);
		peipao.setOnClickListener(this);
		huodong.setOnClickListener(this);
		thv_head_title.getLeftButton().setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				mSuperActivity.menu.showMenu();
			}
		});
		mSuperActivity.menu.setOnCloseListener(new SlidingMenu.OnCloseListener()
		{
			@Override
			public void onClose()
			{
				thv_head_title.getLeftButton().setBackgroundResource(R.drawable.btn_left_normal);
			}
		});
		mSuperActivity.menu.setOnOpenListener(new SlidingMenu.OnOpenListener()
		{
			@Override
			public void onOpen()
			{
				thv_head_title.getLeftButton().setBackgroundResource(R.drawable.btn_left_pressed);
			}
		});
		reqHeadImages();
	}

	private void reqHeadImages()
	{
		HttpUtils http = new HttpUtils();
		urls.clear();
		http.send(HttpMethod.GET, API.headImage, new RequestCallBack<String>()
		{

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo)
			{
				try
				{
					JSONArray imgs = new JSONArray(responseInfo.result);
					for (int i = 0; i < imgs.length(); i++)
					{
						JSONObject img = imgs.getJSONObject(i);
						urls.add(img.getString("PIC_URL"));
					}
					setBannerData(urls);
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(HttpException error, String msg)
			{

			}
		});
	}

	/**
	 * 设置viewpager 数据
	 * 
	 * @param banner
	 */
	private void setBannerData(List<String> banner)
	{
		MyViewPagerAdapter adapter = new MyViewPagerAdapter(banner);
		vp_head.removeAllViews();
		vp_head.setAdapter(adapter);
		// vp_head.getLayoutParams().height = 150;
		setViewpagerPoint();
	}

	/**
	 * 设置viewpager的选中小图标
	 */
	private void setViewpagerPoint()
	{
		if (vp_head == null || vp_head.getAdapter() == null)
			return;
		ll_head_point.removeAllViews();
		LinearLayout.LayoutParams layoutParams4point = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		layoutParams4point.setMargins(0, 0, getResources().getDimensionPixelSize(R.dimen.common_margin), 0);
		for (int i = 0; i < vp_head.getAdapter().getCount(); i++)
		{
			ImageView imageView = new ImageView(activity);
			imageView.setImageResource(R.drawable.dot);
			ll_head_point.addView(imageView, layoutParams4point);
			if (i == 0)
				imageView.setEnabled(false);
			else
				imageView.setEnabled(true);
		}

		vp_head.setOnPageChangeListener(new OnPageChangeListener()
		{
			@Override
			public void onPageSelected(int arg0)
			{
				for (int i = 0; i < ll_head_point.getChildCount(); i++)
				{
					ll_head_point.getChildAt(i).setEnabled(true);
				}
				try
				{
					ll_head_point.getChildAt(arg0).setEnabled(false);
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2)
			{

			}

			@Override
			public void onPageScrollStateChanged(int arg0)
			{

			}
		});
	}

	@Override
	public void onAttach(Activity activity)
	{
		super.onAttach(activity);
		this.activity = activity;
	}

	private class MyViewPagerAdapter extends PagerAdapter
	{
		private List<String> mUrls;

		public MyViewPagerAdapter(List<String> mListViews)
		{
			this.mUrls = mListViews;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object)
		{
			container.removeView((View) object);
			object = null;
		}

		@Override
		public Object instantiateItem(final ViewGroup container, final int position)
		{ // 这个方法用来实例化页卡
			ImageView imageView = new ImageView(activity);
			imageView.setScaleType(ScaleType.FIT_XY);
			imageView.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{

					App.showShortToast("点击了" + position + "路径" + mUrls.get(position).toString());

				}
			});

			ImageLoader.getInstance().displayImage(mUrls.get(position), imageView, mSuperActivity.getDisplayOption());
			container.addView(imageView, 0);
			return imageView;
		}

		@Override
		public int getCount()
		{
			return mUrls.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1)
		{
			return arg0 == arg1;
		}
	}

	@Override
	public void onClick(View v)
	{
		Intent intent;
		switch (v.getId())
		{
		case R.id.dingzuo:// 订座
			intent = new Intent(activity, DingZuoActivity.class);
			App.getInstance().type = BaseConstant.DINGZUO;
			activity.startActivity(intent);
			break;
		case R.id.guahao:// 挂号
			intent = new Intent(activity, DingZuoActivity.class);
			App.getInstance().type = BaseConstant.GUAHAO;
			activity.startActivity(intent);
			break;
		case R.id.waimai:// 外卖
			intent = new Intent(activity, WaiMaiOrderActivity.class);
			App.getInstance().type = BaseConstant.WAIMAI;
			activity.startActivity(intent);

			break;
		case R.id.tongcheng:// 同城
			intent = new Intent(activity, CustomMadeActivity.class);
			App.getInstance().type = BaseConstant.TONGCHENG;
			activity.startActivity(intent);
			break;
		case R.id.laoren:// 老人专区
			intent = new Intent(activity, LaoRenListActivity.class);
			App.getInstance().type = BaseConstant.LAOREN;
			activity.startActivity(intent);
			break;
		case R.id.chongwu:// 遛宠物
			intent = new Intent(activity, JieSongActivity.class);
			App.getInstance().type = BaseConstant.CHONGWU;
			activity.startActivity(intent);
			break;
		case R.id.peipao:// 陪跑
			intent = new Intent(activity, RunActivity.class);
			App.getInstance().type = BaseConstant.PEIPAO;
			activity.startActivity(intent);
			break;
		case R.id.huodong:// 活动
			intent = new Intent(activity, XuYuanActivity.class);
			App.getInstance().type = BaseConstant.XUYUAN;
			activity.startActivity(intent);
			break;

		default:
			break;
		}
	}
}
